package  com.lanyou.cook.web;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.internal.lang.annotation.ajcDeclareAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.entity.domain.InterfaceCount;
import com.lanyou.cook.repository.InterfaceCountRepository;
import com.lanyou.cook.repository.kettle.KettleJobDatabaseRepository;
import com.lanyou.cook.service.security.UserSessionService;
import com.lanyou.cook.web.param.InterfaceCountParam;


@RestController
@RequestMapping(Constants.BASE_URL_API + "/interfaceCounts")
public class InterfaceCountController{
	
	@Autowired
	UserSessionService userSessionService;
	@Autowired
	InterfaceCountRepository interfaceCountRepository;
	@Autowired
	KettleJobDatabaseRepository kettleJobDatabaseRepository;
	
	@RequestMapping("/search/findByExample")
	public Page<InterfaceCount> findByExample(InterfaceCountParam param, Pageable pageable) {

		Specification<InterfaceCount> spec = new Specification<InterfaceCount>() {
			@Override
			public Predicate toPredicate(Root<InterfaceCount> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				List<Predicate> predicatesList = new ArrayList<>();

				if (StringUtils.isNotBlank(param.getInterfaceName())) {
					predicatesList.add(cb.like(root.get("interfaceName"), '%' + param.getInterfaceName() + "%"));
				}
				if (StringUtils.isNoneBlank(param.getFieldName())) {
					predicatesList.add(cb.equal(root.get("fieldName"), param.getFieldName()));
				}
				if (StringUtils.isNoneBlank(param.getSystemName())) {
					predicatesList.add(cb.equal(root.get("systemName"), param.getSystemName()));
				}else{
					Path<String> pathOwnerFieldSystemId = root.get("fieldSystemId");
					// 默认查当前登录用户所管理的领域系统
					Collection<String> fieldSystemIds = userSessionService.findFieldSystemIdsOfCurrentAuthenticatedUserCanManage();
					if(!CollectionUtils.isEmpty(fieldSystemIds)){
						predicatesList.add(pathOwnerFieldSystemId.in(fieldSystemIds));
					}else {
						predicatesList.add(cb.notEqual(cb.literal(1), 1));
					}
				}
				query.where(predicatesList.toArray(new Predicate[predicatesList.size()]));
				return null;
			}
		};
		
		return interfaceCountRepository.findAll(spec, pageable);
	}
	
	
}